local skel = fk.CreateSkill {
  name = "emo__guichen",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__guichen"] = "归尘",
  [":emo__guichen"] = "锁定技，你初始手牌数-2。游戏开始时，从剩余武将堆挑选一张同势力武将替换当前武将牌。",

  ["#emo__guichen-choose"] = "归尘：选择一张武将牌替换当前武将牌",

  ["$emo__guichen1"] = "世间万物，贫道皆可化为其形。",
  ["$emo__guichen2"] = "尘身土塑，唯魂魄难得。",
}

skel:addEffect(fk.GameStart, {
  anim_type = "big",
  priority = 1.01,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local generals = table.filter(room.general_pile, function (g)
      local general = Fk.generals[g]
      return general.kingdom == player.kingdom or general.subkingdom == player.kingdom
    end)
    if #generals == 0 then return end
    local result = room:askToCustomDialog(player, {
      skill_name = skel.name,
      qml_path = "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml",
      extra_data = {
        generals,
        {"OK"},
        "#emo__guichen-choose",
        {},
        1,
        1,
        {},
      },
    })
    local general = generals[1]
    if result ~= "" then
      if result.choice == "OK" then
        general = result.cards[1]
      end
    end
    room:findGeneral(general)
    -- 副将直接删了，懒得处理
    room:changeHero(player, "", true, true, false, false)
    room:changeHero(player, general, true, false, true, true, false)
    local g = Fk.generals[general]
    local hp = player.maxHp - (g.maxHp - g.hp)
    if hp < player.maxHp then -- 初始hp也没计算正确，服
      room:setPlayerProperty(player, "hp", hp)
    end
    if g.shield > player.shield then -- 这个变身不会变护甲，手动加
      room:changeShield(player, g.shield - player.shield)
    end
  end,
})

skel:addEffect(fk.DrawInitialCards, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
  end,
  on_refresh = function (self, event, target, player, data)
    data.num = math.max(0, data.num - 2)
  end,
})

return skel
